package com.yc.service;

import com.yc.bean.Account;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Date;

/**
 * @author zwei
 * @create 2024-08-11 18:59
 */
@Component
public class VelocityTemplateBizImpl {      //开发一个模板引擎  读取模板  并将模板中的数据填充到模板中
    @Autowired
    private VelocityEngine velocityEngine;  // 模板引擎
    @Autowired
    private VelocityContext context;        // 模板上下文
    @Autowired
    @Qualifier("depositTemplate")
    private Template depositTemplate;
    @Autowired
    @Qualifier("withdrawTemplate")
    private Template withdrawTemplate;
    @Autowired
    @Qualifier("transferTemplate")
    private Template tranferTemplate;

    @Autowired
    @Qualifier("fullDf")
    private DateFormat fullDf;

    @Autowired
    @Qualifier("partDf")
    private DateFormat partDf;
    //生成邮件内容
    public String getEmailContext(String opType,Account account,double money,int toaccountid){
        String info;
        if (opType.equals("deposite")){
            info=depositeInfo(account,money);
        } else if (opType.equals("withdraw")) {
            info=withdrawInfo(account,money);
        }else if (opType.equals("transfer")) {
            info=transferInfo(account,money,toaccountid);
        }else {
            info="";
        }
        return info;
    }
    //生成转账邮件内容
    private String transferInfo(Account account, double money, int toaccountid) {
//        Date d=new Date();
//        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        String dstring=df.format(d);
//        String s="尊敬的%s先生:\n\t您的账户%s于%s向账户%s转账了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
//        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,toaccountid,money,account.getBalance());
        Date d=new Date();
//        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");
//
//        //后面使用spring托管
//        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
//        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
//        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
//        velocityEngine.init();

        //托管
//        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
        context.put("accountid",account.getAccountid());
        context.put("email",account.getEmail());
        context.put("subject","存款操作通知");
        context.put("optime",fullDf.format(d));
        context.put("money",money);
        context.put("balance",account.getBalance());
        context.put("currentDate",partDf.format(d));
        //Merge template and context
//        Template template=velocityEngine.getTemplate("vms/transfer.vm","utf-8");
        try {
            StringWriter writer=new StringWriter(); //合并内容 替换占位符
            tranferTemplate.merge(context,writer);         //从流获取最终的字符串
            return writer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    //生成取款邮件内容
    private String withdrawInfo(Account account, double money) {
//        Date d=new Date();
//        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        String dstring=df.format(d);
//        String s="尊敬的%s先生:\n\t您的账户%s于%s取出了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
//        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,money,account.getBalance());
        Date d=new Date();
//        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");

        //后面使用spring托管
//        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
//        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
//        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
//        velocityEngine.init();

        //托管
//        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
        context.put("accountid",account.getAccountid());
        context.put("email",account.getEmail());
        context.put("subject","取款操作通知");
        context.put("optime",fullDf.format(d));
        context.put("money",money);
        context.put("balance",account.getBalance());
        context.put("currentDate",partDf.format(d));

        //Merge template and context
//        Template template=velocityEngine.getTemplate("vms/withdraw.vm","utf-8");
        try {
            StringWriter writer=new StringWriter(); //合并内容 替换占位符
            withdrawTemplate.merge(context,writer);         //从流获取最终的字符串
            return writer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    //生成存款邮件内容
    private String depositeInfo(Account account, double money){
        //拼接文档
//        Date d=new Date();
//        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        String dstring=df.format(d);
//        String s="尊敬的%s先生:\n\t您的账户%s于%s存入了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
//        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,money,account.getBalance());
        Date d=new Date();
//        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");

        //后面使用spring托管
//        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
//        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
//        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
//        velocityEngine.init();

        //托管
//        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
        context.put("accountid",account.getAccountid());
        context.put("email",account.getEmail());
        context.put("subject","存款操作通知");
        context.put("optime",fullDf.format(d));
        context.put("money",money);
        context.put("balance",account.getBalance());
        context.put("currentDate",partDf.format(d));

        //Merge template and context
//        Template template=velocityEngine.getTemplate("vms/deposite.vm","utf-8");
        try {
            StringWriter writer=new StringWriter(); //合并内容 替换占位符
            depositTemplate.merge(context,writer);         //从流获取最终的字符串
            return writer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
